﻿using System;
using System.Collections.Generic;
using System.Data;
using NHibernate;
using MySSH.IDAL;
using MySSH.Domain.Entities;


namespace MySSH.DAL
{
    public partial class DALModule : DALBase<Module>, IDALModule
    {

        public System.Data.DataSet GetSysMenuByUser(int userID, string loginID, string pwd)
        {
            List<Module> list = new List<Module>();
            //DetachedCriteria query = Restrictions BuildCriteria()

            IQuery iq = base.Session.GetNamedQuery("SysGetSysMenu").SetInt32("iUserID", userID).SetString("sLoginID", loginID).SetString("sUserPwd", pwd);

            var d = iq.List();




            IDbConnection conn = base.Session.Connection;
            IDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "SysGetSysMenu";
            cmd.CommandType = CommandType.StoredProcedure;
            
            IDbDataAdapter da = new System.Data.SqlClient.SqlDataAdapter((System.Data.SqlClient.SqlCommand)cmd);
            System.Data.SqlClient.SqlParameter[] param = {  new System.Data.SqlClient.SqlParameter("@iUserID", userID),
                                                            new System.Data.SqlClient.SqlParameter("@sLoginID",loginID),
                                                            new System.Data.SqlClient.SqlParameter("@sUserPwd",pwd)
                                                         };
            Array.ForEach<System.Data.SqlClient.SqlParameter>(param, p => cmd.Parameters.Add(p));
            System.Data.DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;


            //return this.HibernateTemplate.ExecuteFind<System.Data.DataSet>(delegate(NHibernate.ISession session)
            // {
            //     IList<DataSet> dsList = new List<DataSet>();
            //     NHibernate.IQuery iq = session.GetNamedQuery("SysGetSysMenu").SetInt32("iUserID", userID).SetString("sLoginID", loginID).SetString("sUserPwd", pwd);

            //     IDbConnection conn = Session.Connection;
            //     IDbCommand cmd = conn.CreateCommand();
            //     //if (session.Transaction.IsActive)
            //     //{
            //     //    session.Transaction.Enlist(cmd);
            //     //}
            //     //else
            //     //{
            //     //    session.BeginTransaction();
            //     //    session.Transaction.Enlist(cmd);
            //     //}
            //     IDbDataAdapter da = new System.Data.SqlClient.SqlDataAdapter((System.Data.SqlClient.SqlCommand)cmd);
            //     cmd.CommandText = "SysGetSysMenu";
            //     cmd.CommandType = CommandType.StoredProcedure;
            //     System.Data.SqlClient.SqlParameter[] param = { 
            //                                                     new System.Data.SqlClient.SqlParameter("@iUserID", userID),
            //                                                     new System.Data.SqlClient.SqlParameter("@sLoginID",loginID),
            //                                                 new System.Data.SqlClient.SqlParameter("@sUserPwd",pwd)};
            //     Array.ForEach<System.Data.SqlClient.SqlParameter>(param, p => cmd.Parameters.Add(p));
            //     System.Data.DataSet ds = new DataSet();
            //     da.Fill(ds);
            //     dsList.Add(ds);
            //     return dsList;
            // }).[0];



        }

        public void ProcTest()
        {
            //IDbConnection conn = base.Session.Connection;
            //IDbCommand cmd = conn.CreateCommand();

            //NHibernate.SqlCommand.SqlString m_SqlString = new NHibernate.SqlCommand.SqlString("getIDSeed");
            //NHibernate.SqlTypes.SqlType[] arrTypes = new NHibernate.SqlTypes.SqlType[] { };
            //IDbDataParameter m_Param_1 = cmd.CreateParameter();
            //m_Param_1.ParameterName = "description";
            //m_Param_1.Value = 1;
            //cmd.Parameters.Add(m_Param_1);
            //cmd.ExecuteReader();
            //cmd.ExecuteReader


        }

        public System.Data.DataTable GetModelByParentID(int parentID)
        {
            throw new NotImplementedException();
        }
    }
}
